package org.hy.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.persistence.FilePersistenceStrategy;
import com.thoughtworks.xstream.persistence.XmlArrayList;

/**
 * 对javabean进行xml序列化及反序列化
 * @author xwm
 *
 */
public class XMLUtil {
private static XStream stream = new XStream();
	
	
	@SuppressWarnings("unchecked")
	public static <T>  List<T> getListDataFromXml(String path,Class<T> clazz) {
		if(path==null)return null;
		File file = new File(path);
		Object data = null;
		
		try {
			data = stream.fromXML(file);
		} catch (Exception e) {
			throw new RuntimeException("解析路径"+path+"处的xml文件错误");
		}
		List dataList = new ArrayList(1);
		
		if(data instanceof List){
			dataList = (List)data;
		}else{
			dataList.add(data);
		}
		if(clazz != null)return (List<T>)dataList;
		else return dataList;
	}
	
	public static List getListDataFromXml(String path){
		return getListDataFromXml(path, null);
	}
	
	public static void persisDataToXml(Object data,String dir) {
		if(data == null || dir == null)return;
		File f = new File(dir);
		if(!f.exists()){
			f.mkdirs();
		}
		XmlArrayList xmlList = new XmlArrayList(new FilePersistenceStrategy(f));
		List dataList = new ArrayList(1); 
		if(data == null) return;
		if(!(data instanceof List)){
			dataList.add(data);
		}else{
			dataList = (List)data;
		}
		xmlList.add(dataList);
		
	}
	
	public static  List<Object[]> getTestDataFromXml(String path) {
		List dataList = getListDataFromXml(path);
		if(dataList ==  null) return new ArrayList<Object[]>();
		List<Object[]> newDataList = new ArrayList<Object[]>(dataList.size());
		Object[] newItem = null;
		for (Object item : dataList) {
			newItem = new Object[1];
			newItem[0] = item;
			newDataList.add(newItem);
		}
		return newDataList;
	}
	
	@Deprecated
	public static <T> T[] splite(String str,String separator,Class<T> cls) {
		if(str == null)return null;
		String [] items = str.split(separator);
		boolean canConvertInt = true;
		String sitem = null;
		Number numItem = null;
		Number[] numItems = new Number[items.length];
		Integer[] intRes = null;
		Double[] doubleRes = null;
		int len = items.length;
		if(!cls.isPrimitive()) return (T[])items;
		for (int i = 0 ; i < items.length;i++) {
			try{  //类型转换
				sitem = items[i];
				if(canConvertInt)numItem = Integer.valueOf(sitem);
				else numItem = Double.valueOf(sitem);
				numItems[i] = numItem;
			}catch (Exception e) {
				if(canConvertInt){//Int类型转换失败
					canConvertInt = false;
					i--;  //用Double类型转换
				}else {  //Double类型转换失败
					return (T[])items;
				}
			}
			
		}
		
		if(canConvertInt){
			intRes = new Integer[len];
			System.arraycopy(numItems, 0, intRes, 0, len);
			return (T[])intRes;
		}else {
			doubleRes = new Double[len];
			System.arraycopy(numItems, 0, doubleRes, 0, len);
			return (T[])doubleRes;
		}
		
	}
}
